package cn.sccl.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import cn.sccl.dao.UserDao;
import cn.sccl.domain.User;

/**
 * @author 饶爽
 * @time 2019年9月9日 下午9:29:00
 * @version 1.0.0
 * User访问数据库实现类
*/
//HibernateDaoSupport 为dao注入sessionFactory
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
    
    @Override
    public User getByUserCode(final String usercode) {
        //HQL
        return getHibernateTemplate().execute(new HibernateCallback<User>() {
            @Override
            public User doInHibernate(Session session) throws HibernateException {
                    String hql = "from User where user_code = ? ";
                    Query query = session.createQuery(hql);
                    query.setParameter(0, usercode);
                    User user = (User) query.uniqueResult();
                return user;
            }
        });
        //Criteria
        /*DetachedCriteria dc = DetachedCriteria.forClass(User.class);
        dc.add(Restrictions.eq("user_code", usercode));
        List<User> list = (List<User>) getHibernateTemplate().findByCriteria(dc);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }else {
            return null;
        }*/
    }

    @Override
    public void save(User u) {
        getHibernateTemplate().save(u);
    }

}
